<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>产品对比分析 - 跨境电商竞品分析系统</title>
    <!-- Bootstrap 5 CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome 图标 -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css">
    <!-- Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
    <!-- 传递服务器设置到前端 JavaScript -->
    <script>
        window.defaultProductImage = "{{ default_product_image }}";
        window.fallbackImageUrl = "{{ fallback_image_url }}";
    </script>
    <style>
        :root {
            --primary-color: #5e72e4;
            --primary-dark: #4a5fd0;
            --secondary-color: #ff7eb3;
            --accent-color: #11cdef;
            --light-color: #f8f9fe;
            --dark-color: #32325d;
            --success-color: #2dcb73;
            --warning-color: #fb6340;
            --danger-color: #f5365c;
            --info-color: #6a9ef7;
            --neutral-color: #8898aa;
            --pink-gradient: linear-gradient(120deg, #f093fb 0%, #f5576c 100%);
            --blue-gradient: linear-gradient(135deg, #5e72e4 0%, #825ee4 100%);
            --green-gradient: linear-gradient(120deg, #84fab0 0%, #8fd3f4 100%);
        }
        body {
            background-color: #f8f9fe;
            font-family: 'Nunito', 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
            color: var(--dark-color);
        }
        .page-header {
            background: var(--blue-gradient);
            color: white;
            padding: 2.5rem 0;
            position: relative;
            overflow: hidden;
            margin-bottom: 2rem;
        }
        .page-header::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-image: url('https://img.freepik.com/free-vector/network-mesh-wire-digital-technology-background_1017-27428.jpg');
            background-size: cover;
            background-position: center;
            opacity: 0.1;
            z-index: 0;
        }
        .page-header-content {
            position: relative;
            z-index: 1;
        }
        .circle {
            position: absolute;
            border-radius: 50%;
            background: rgba(255, 255, 255, 0.05);
        }
        .circle-1 {
            width: 200px;
            height: 200px;
            top: -50px;
            right: -30px;
        }
        .circle-2 {
            width: 100px;
            height: 100px;
            bottom: -20px;
            left: 10%;
        }
        .navbar-brand {
            font-weight: 800;
            color: white;
            font-size: 1.4rem;
        }
        
        /* 对比表格样式 */
        .compare-table {
            background: white;
            border-radius: 15px;
            overflow: hidden;
            box-shadow: 0 10px 25px rgba(50, 50, 93, 0.1), 0 5px 15px rgba(0, 0, 0, 0.07);
        }
        .compare-table th {
            background-color: var(--light-color);
            font-weight: 700;
            padding: 16px;
            border-bottom: 2px solid rgba(0,0,0,0.05);
        }
        .compare-table td {
            padding: 16px;
            border-bottom: 1px solid rgba(0,0,0,0.05);
        }
        .compare-header {
            text-align: center;
            padding: 20px;
            border-bottom: 1px solid rgba(0,0,0,0.05);
        }
        .product-image-header {
            width: 150px;
            height: 150px;
            margin: 0 auto 15px;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .product-image-header img {
            width: 100%;
            height: 100%;
            object-fit: cover;
        }
        .feature-category {
            background-color: var(--light-color);
            font-weight: 700;
            color: var(--primary-color);
        }
        .feature-highlight {
            background-color: rgba(94, 114, 228, 0.1);
        }
        
        /* 产品对比卡片样式 */
        .product-compare-card {
            background: white;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            margin-bottom: 20px;
            position: relative;
        }
        .product-compare-header {
            background: var(--light-color);
            padding: 15px;
            font-weight: 700;
            border-bottom: 1px solid rgba(0,0,0,0.05);
            display: flex;
            align-items: center;
            justify-content: space-between;
        }
        .product-compare-body {
            padding: 15px;
        }
        .product-feature {
            margin-bottom: 15px;
            border-bottom: 1px solid rgba(0,0,0,0.05);
            padding-bottom: 15px;
        }
        .product-feature:last-child {
            margin-bottom: 0;
            border-bottom: none;
            padding-bottom: 0;
        }
        .feature-name {
            font-weight: 600;
            margin-bottom: 5px;
            display: flex;
            align-items: center;
        }
        .feature-icon {
            width: 25px;
            height: 25px;
            background: var(--light-color);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 10px;
        }
        .feature-value {
            color: var(--neutral-color);
        }
        
        /* 优势分析卡片样式 */
        .advantage-card {
            background: white;
            border-radius: 10px;
            overflow: hidden;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            margin-bottom: 20px;
        }
        .advantage-header {
            padding: 15px;
            font-weight: 700;
            border-bottom: 1px solid rgba(0,0,0,0.05);
            background: var(--light-color);
        }
        .advantage-body {
            padding: 15px;
        }
        .advantage-list {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .advantage-item {
            margin-bottom: 10px;
            padding-bottom: 10px;
            border-bottom: 1px solid rgba(0,0,0,0.05);
            display: flex;
            align-items: flex-start;
        }
        .advantage-item:last-child {
            margin-bottom: 0;
            border-bottom: none;
            padding-bottom: 0;
        }
        .advantage-icon {
            margin-right: 10px;
            color: var(--success-color);
            font-size: 1.2rem;
            margin-top: 2px;
        }
        
        /* 假设产品样式 */
        .hypothetical-product {
            background: linear-gradient(to right, rgba(132, 250, 176, 0.1), rgba(143, 211, 244, 0.1));
            border-left: 3px solid var(--success-color);
        }
        
        /* 评分图表样式 */
        .chart-container {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
            height: 400px; /* 固定图表容器高度 */
        }
        
        /* 产品选择卡片样式 */
        .product-card {
            background: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(50, 50, 93, 0.1), 0 5px 8px rgba(0, 0, 0, 0.07);
            transition: all 0.3s ease;
            overflow: hidden;
            height: 100%;
            cursor: pointer;
            border: 2px solid transparent;
            position: relative;
        }
        .product-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 15px 35px rgba(50, 50, 93, 0.15), 0 10px 25px rgba(0, 0, 0, 0.1);
        }
        .product-card.selected {
            border-color: var(--primary-color);
            box-shadow: 0 8px 25px rgba(94, 114, 228, 0.3);
        }
        .product-image {
            height: 180px;
            overflow: hidden;
        }
        .product-image img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.5s ease;
        }
        .product-card:hover .product-image img {
            transform: scale(1.1);
        }
        .product-content {
            padding: 1.25rem;
        }
        .product-title {
            font-weight: 700;
            margin-bottom: 0.5rem;
            color: var(--dark-color);
        }
        .product-description {
            color: var(--neutral-color);
            font-size: 0.9rem;
            margin-bottom: 0;
        }
        .product-checkbox {
            position: absolute;
            top: 10px;
            left: 10px;
            width: 28px;
            height: 28px;
            background: rgba(255,255,255,0.95);
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            z-index: 10;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        /* 分析结果样式 */
        .analysis-section {
            background: white;
            border-radius: 15px;
            padding: 2rem;
            margin-bottom: 2rem;
            box-shadow: 0 5px 15px rgba(0,0,0,0.1);
        }
        .analysis-header {
            border-bottom: 2px solid var(--light-color);
            padding-bottom: 1rem;
            margin-bottom: 1.5rem;
            display: flex;
            align-items: center;
        }
        .analysis-header h4 {
            margin-bottom: 0;
            font-weight: 700;
            color: var(--primary-color);
        }
        .analysis-content {
            line-height: 1.8;
        }
        .analysis-content h3 {
            color: var(--primary-color);
            margin: 1.5rem 0 1rem;
            font-size: 1.4rem;
            font-weight: 700;
            border-bottom: 1px solid rgba(94, 114, 228, 0.1);
            padding-bottom: 0.5rem;
        }
        .analysis-content h4 {
            color: var(--primary-color);
            margin: 1.2rem 0 0.8rem;
            font-size: 1.2rem;
            font-weight: 600;
        }
        .analysis-content p {
            margin-bottom: 1rem;
        }
        .analysis-content ul {
            padding-left: 1.5rem;
            margin-bottom: 1.5rem;
            list-style-type: disc;
        }
        .analysis-content li {
            margin-bottom: 0.8rem;
            line-height: 1.6;
        }
        .analysis-content strong {
            color: var(--primary-color);
            font-weight: 600;
        }
        .analysis-content .highlight {
            background-color: rgba(94, 114, 228, 0.1);
            padding: 0.2rem 0.5rem;
            border-radius: 4px;
            color: var(--primary-color);
        }
        .analysis-content .tag {
            display: inline-block;
            background: var(--light-color);
            color: var(--primary-color);
            padding: 0.2rem 0.8rem;
            border-radius: 20px;
            margin: 0.2rem;
            font-size: 0.9rem;
        }
        
        /* 章节对比样式 */
        .section-compare {
            margin-bottom: 3rem;
        }
        .section-title {
            color: var(--primary-color);
            font-weight: 700;
            margin-bottom: 1.5rem;
            padding-bottom: 0.7rem;
            border-bottom: 2px solid rgba(94, 114, 228, 0.2);
        }
        .section-content {
            background: white;
            border-radius: 12px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            overflow: hidden;
        }
        .compare-row {
            display: flex;
            border-bottom: 1px solid rgba(0,0,0,0.05);
        }
        .compare-row:last-child {
            border-bottom: none;
        }
        .compare-item {
            flex: 1;
            padding: 1.5rem;
            border-right: 1px solid rgba(0,0,0,0.05);
        }
        .compare-item:last-child {
            border-right: none;
        }
        .compare-item-header {
            display: flex;
            align-items: center;
            margin-bottom: 1rem;
        }
        .compare-item-header img {
            width: 40px;
            height: 40px;
            object-fit: cover;
            border-radius: 50%;
            margin-right: 1rem;
        }
        .compare-item-header h5 {
            margin: 0;
            font-weight: 600;
        }
        .compare-item-content {
            line-height: 1.6;
        }
        .compare-item-content ul {
            padding-left: 1.5rem;
            margin-bottom: 0;
        }
        .compare-item-content li {
            margin-bottom: 0.8rem;
        }
        .compare-item-content li:last-child {
            margin-bottom: 0;
        }
    </style>
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark" style="background: var(--dark-color);">
        <div class="container">
            <a class="navbar-brand" href="/">
                <i class="fas fa-globe-asia me-2"></i>竞品分析系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav ms-auto">
                    <li class="nav-item">
                        <a class="nav-link" href="/products">产品列表</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="/create-product">创建分析</a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link active" href="/compare-products">产品对比</a>
                    </li>
                </ul>
            </div>
        </div>
    </nav>

    <!-- 页面头部 -->
    <div class="page-header">
        <div class="circle circle-1"></div>
        <div class="circle circle-2"></div>
        <div class="container page-header-content">
            <div class="row align-items-center">
                <div class="col-lg-8">
                    <h1 class="fw-bold mb-2">产品竞争分析对比</h1>
                    <p class="lead mb-0">直观对比多个产品特征，把握市场竞争态势</p>
                </div>
                <div class="col-lg-4 text-lg-end mt-3 mt-lg-0">
                    <a href="/products" class="btn btn-light">
                        <i class="fas fa-arrow-left me-2"></i>返回产品列表
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- 主内容区 -->
    <div class="container mb-5">
        <!-- 产品加载提示 -->
        <div id="loadingIndicator" class="text-center py-5">
            <div class="spinner-border text-primary" role="status"></div>
            <p class="mt-3">正在加载产品对比数据...</p>
        </div>
        
        <!-- 对比内容区域 -->
        <div id="compareContent" style="display: none;">
            <!-- 综合评分对比图表 -->
            <div class="row mb-4">
                <div class="col-12">
                    <div class="chart-container">
                        <h5 class="mb-3">竞品综合评分对比</h5>
                        <canvas id="scoreChart" height="120"></canvas>
                    </div>
                </div>
            </div>
            
            <!-- 章节式对比 -->
            <div id="sectionCompare">
                <!-- 各章节对比将动态生成 -->
            </div>
        </div>
        
        <!-- 产品选择UI -->
        <div id="productSelectionUI" style="display: none;">
            <div class="row mb-4">
                <div class="col-12">
                    <div class="alert alert-info">
                        <i class="fas fa-info-circle me-2"></i>
                        请选择2-4个已完成分析的产品进行对比分析
                    </div>
                </div>
            </div>
            <div class="row mb-4">
                <div class="col-md-6">
                    <div class="input-group">
                        <span class="input-group-text bg-white border-end-0">
                            <i class="fas fa-search text-muted"></i>
                        </span>
                        <input type="text" id="searchInput" class="form-control border-start-0" placeholder="搜索产品名称...">
                    </div>
                </div>
                <div class="col-md-6 text-md-end mt-3 mt-md-0">
                    <button id="startCompareBtn" disabled class="btn btn-primary">
                        <i class="fas fa-exchange-alt me-1"></i> 开始对比分析
                    </button>
                </div>
            </div>
            <div id="productSelectionList" class="row">
                <div class="col-12 text-center py-5">
                    <div class="spinner-border text-primary" role="status"></div>
                    <p class="mt-3">加载产品中...</p>
                </div>
            </div>
        </div>
        
        <!-- 错误提示 -->
        <div id="errorMessage" class="alert alert-danger" style="display: none;">
            <i class="fas fa-exclamation-circle me-2"></i>
            <span id="errorText">加载产品对比数据失败</span>
        </div>
    </div>

    <!-- Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/js/bootstrap.bundle.min.js"></script>
    
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            // 获取URL参数中的产品ID列表
            const urlParams = new URLSearchParams(window.location.search);
            const productIds = urlParams.get('products') ? urlParams.get('products').split(',') : [];
            
            // 存储选中的产品ID
            let selectedProducts = [...productIds];
            
            // 获取系统配置的默认图片和备用图片URL
            // 如果服务器没有提供则使用前端默认值
            const defaultProductImage = window.defaultProductImage || "Screenshot_20250416_121955.jpg";
            const fallbackImageUrl = window.fallbackImageUrl || "https://img.freepik.com/free-vector/image-upload-concept-illustration_114360-798.jpg";
            
            // 如果已有选定的产品，则直接开始对比
            if (selectedProducts.length >= 2) {
                loadComparisonData(selectedProducts);
            } else {
                // 否则显示产品选择界面
                loadAvailableProducts();
            }
        });
        
        // 显示错误信息
        function showError(message) {
            document.getElementById('loadingIndicator').style.display = 'none';
            document.getElementById('errorMessage').style.display = 'block';
            document.getElementById('errorText').textContent = message;
        }
        
        // 显示产品选择界面
        function showProductSelectionUI() {
            document.getElementById('loadingIndicator').style.display = 'none';
            document.getElementById('productSelectionUI').style.display = 'block';
            
            // 加载所有产品
            loadProductsForSelection();
            
            // 添加搜索功能
            const searchInput = document.getElementById('searchInput');
            if (searchInput) {
                searchInput.addEventListener('keyup', function() {
                    const searchValue = this.value.toLowerCase();
                    const productCards = document.querySelectorAll('.product-selection-card');
                    
                    productCards.forEach(card => {
                        const productName = card.querySelector('.product-title').textContent.toLowerCase();
                        if (productName.includes(searchValue)) {
                            card.style.display = 'block';
                        } else {
                            card.style.display = 'none';
                        }
                    });
                });
            }
        }
        
        // 存储选择的产品ID
        const selectedProducts = [];
        const MAX_PRODUCTS = 4;
        
        // 加载产品列表供选择
        function loadProductsForSelection() {
            fetch('/api/products')
                .then(response => {
                    if (!response.ok) {
                        throw new Error('获取产品列表失败');
                    }
                    return response.json();
                })
                .then(products => {
                    renderProductsForSelection(products);
                })
                .catch(error => {
                    console.error('Error:', error);
                    const productList = document.getElementById('productSelectionList');
                    if (productList) {
                        productList.innerHTML = `
                            <div class="col-12 text-center py-5">
                                <div class="alert alert-danger" role="alert">
                                    加载失败: ${error.message}
                                </div>
                            </div>
                        `;
                    }
                });
        }
        
        // 处理产品选择
        function handleProductSelect(productId) {
            const index = selectedProducts.indexOf(productId);
            
            if (index > -1) {
                // 取消选择
                selectedProducts.splice(index, 1);
            } else {
                // 添加选择
                if (selectedProducts.length >= MAX_PRODUCTS) {
                    alert(`最多可以选择 ${MAX_PRODUCTS} 个产品进行对比`);
                    return;
                }
                selectedProducts.push(productId);
            }
            
            // 更新UI显示
            updateProductSelectionUI();
        }
        
        // 更新产品选择UI
        function updateProductSelectionUI() {
            const cards = document.querySelectorAll('.product-card');
            cards.forEach(card => {
                const productId = card.getAttribute('data-product-id');
                const isSelected = selectedProducts.includes(productId);
                
                // 更新卡片样式
                if (isSelected) {
                    card.classList.add('selected');
                } else {
                    card.classList.remove('selected');
                }
                
                // 更新勾选图标
                const checkIcon = card.querySelector('.product-checkbox i');
                if (checkIcon) {
                    checkIcon.className = isSelected ? 'fas fa-check-circle text-primary' : 'fas fa-circle text-muted';
                }
            });
            
            // 更新开始对比按钮状态
            const startCompareBtn = document.getElementById('startCompareBtn');
            if (startCompareBtn) {
                startCompareBtn.disabled = selectedProducts.length < 2;
                startCompareBtn.innerHTML = `<i class="fas fa-exchange-alt me-1"></i> 开始对比分析 (${selectedProducts.length})`;
            }
        }
        
        // 渲染产品选择列表
        function renderProductsForSelection(products) {
            const productList = document.getElementById('productSelectionList');
            if (!productList) return;
            
            productList.innerHTML = '';
            
            // 筛选已完成分析的产品
            const analyzedProducts = products.filter(p => p.has_analysis);
            
            if (analyzedProducts.length === 0) {
                productList.innerHTML = `
                    <div class="col-12 text-center py-5">
                        <div class="alert alert-warning" role="alert">
                            没有找到已完成分析的产品，请先完成产品分析
                        </div>
                    </div>
                `;
                return;
            }
            
            // 渲染产品卡片
            analyzedProducts.forEach(product => {
                const imagePath = product.image_count > 0 
                    ? `/static/uploads/products/${product.id}/${product.cover_image || defaultProductImage}`
                    : fallbackImageUrl;
                
                const col = document.createElement('div');
                col.className = 'col-md-3 mb-4 product-selection-card';
                
                const isSelected = selectedProducts.includes(product.id);
                
                col.innerHTML = `
                    <div class="product-card ${isSelected ? 'selected' : ''}" data-product-id="${product.id}">
                        <div class="product-checkbox">
                            <i class="fas ${isSelected ? 'fa-check-circle text-primary' : 'fa-circle text-muted'}" style="font-size: 20px;"></i>
                        </div>
                        <div class="product-image">
                            <img src="${imagePath}" alt="${product.name}" 
                                 onerror="this.src='${fallbackImageUrl}'">
                        </div>
                        <div class="product-content">
                            <h5 class="product-title">${product.name}</h5>
                            <p class="product-description">已完成分析，可进行对比。</p>
                        </div>
                    </div>
                `;
                productList.appendChild(col);
                
                // 添加选择事件
                const card = col.querySelector('.product-card');
                card.addEventListener('click', function() {
                    handleProductSelect(product.id);
                });
            });
            
            // 添加开始对比按钮事件
            const startCompareBtn = document.getElementById('startCompareBtn');
            if (startCompareBtn) {
                startCompareBtn.addEventListener('click', function() {
                    if (selectedProducts.length >= 2) {
                        window.location.href = '/compare-products?products=' + selectedProducts.join(',');
                    }
                });
            }
        }
        
        // 加载产品对比数据
        function loadComparisonData(productIds) {
            fetch(`/api/compare-products?products=${productIds.join(',')}`)
                .then(response => {
                    if (!response.ok) {
                        throw new Error('获取对比数据失败');
                    }
                    return response.json();
                })
                .then(data => {
                    document.getElementById('loadingIndicator').style.display = 'none';
                    document.getElementById('compareContent').style.display = 'block';
                    
                    renderSectionCompare(data.products);
                    renderScoreChart(data.products);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showError(error.message);
                });
        }
        
        // 渲染评分图表
        function renderScoreChart(products) {
            const ctx = document.getElementById('scoreChart').getContext('2d');
            
            // 准备图表数据
            const labels = products.map(p => p.name);
            const scores = products.map(p => p.scores || {});
            
            // 提取所有评分类别
            const allCategories = new Set();
            scores.forEach(score => {
                Object.keys(score).forEach(category => allCategories.add(category));
            });
            
            const datasets = Array.from(allCategories).map((category, index) => {
                const colors = [
                    'rgba(94, 114, 228, 0.8)',
                    'rgba(245, 87, 108, 0.8)',
                    'rgba(17, 205, 239, 0.8)',
                    'rgba(45, 203, 115, 0.8)',
                    'rgba(251, 99, 64, 0.8)'
                ];
                
                return {
                    label: category,
                    data: scores.map(score => score[category] || 0),
                    backgroundColor: colors[index % colors.length],
                    borderWidth: 0,
                    borderRadius: 5,
                    maxBarThickness: 35
                };
            });
            
            // 创建图表
            new Chart(ctx, {
                type: 'bar',
                data: {
                    labels: labels,
                    datasets: datasets
                },
                options: {
                    responsive: true,
                    maintainAspectRatio: false,
                    scales: {
                        y: {
                            beginAtZero: true,
                            max: 10,
                            ticks: {
                                stepSize: 2
                            }
                        }
                    },
                    plugins: {
                        legend: {
                            position: 'top',
                        }
                    }
                }
            });
        }
        
        // 渲染章节式对比
        function renderSectionCompare(products) {
            const container = document.getElementById('sectionCompare');
            container.innerHTML = '';
            
            // 确保所有产品都有分析结果
            const productsWithAnalysis = products.filter(p => p.analysis && p.analysis.analysis_result);
            if (productsWithAnalysis.length < 2) return;
            
            // 定义要比较的章节
            const sections = [
                {id: "主要功能特点", title: "主要功能特点"},
                {id: "技术参数", title: "技术参数"},
                {id: "市场优势", title: "市场竞争优势"},
                {id: "竞品分析", title: "竞品分析"},
                {id: "核心亮点总结", title: "核心亮点总结"}
            ];
            
            // 为每个章节创建对比区域
            sections.forEach(section => {
                const sectionDiv = document.createElement('div');
                sectionDiv.className = 'section-compare';
                
                // 章节标题
                const title = document.createElement('h4');
                title.className = 'section-title';
                title.textContent = section.title;
                sectionDiv.appendChild(title);
                
                // 章节内容容器
                const content = document.createElement('div');
                content.className = 'section-content';
                
                // 提取所有产品的章节内容
                const sectionContents = productsWithAnalysis.map(product => {
                    const analysis = product.analysis.analysis_result;
                    const sectionRegex = new RegExp(`### ${section.id}([\\s\\S]*?)(?=###|$)`, 'i');
                    const match = analysis.match(sectionRegex);
                    return {
                        product: product,
                        content: match ? match[1].trim() : ''
                    };
                });
                
                // 将内容按行分割并创建对比行
                const maxLines = Math.max(...sectionContents.map(sc => {
                    return sc.content.split('\n').filter(line => line.trim()).length;
                }));
                
                for (let i = 0; i < maxLines; i++) {
                    const row = document.createElement('div');
                    row.className = 'compare-row';
                    
                    sectionContents.forEach(sc => {
                        const lines = sc.content.split('\n').filter(line => line.trim());
                        const line = lines[i] || '';
                        
                        const item = document.createElement('div');
                        item.className = 'compare-item';
                        
                        // 如果是第一行，添加产品标题
                        if (i === 0) {
                            const header = document.createElement('div');
                            header.className = 'compare-item-header';
                            header.innerHTML = `
                                <img src="/static/uploads/products/${sc.product.id}/${sc.product.image || defaultProductImage}" 
                                     alt="${sc.product.name}"
                                     onerror="this.src='${fallbackImageUrl}'">
                                <h5>${sc.product.name}</h5>
                            `;
                            item.appendChild(header);
                        }
                        
                        // 添加内容
                        const content = document.createElement('div');
                        content.className = 'compare-item-content';
                        
                        // 处理内容格式
                        let formattedLine = line
                            .replace(/- \*\*([^*]+)\*\*：/g, '<strong>$1</strong>：')
                            .replace(/- /g, '• ');
                            
                        content.innerHTML = formattedLine;
                        item.appendChild(content);
                        
                        row.appendChild(item);
                    });
                    
                    content.appendChild(row);
                }
                
                sectionDiv.appendChild(content);
                container.appendChild(sectionDiv);
            });
        }
    </script>
</body>
</html> 